#include "CharacterClient.h"

#include <iostream>

using namespace std;

sf::Packet& operator >>(sf::Packet& packet, CharacterClient& characterClient) {

	int dir, act;
	packet >> characterClient.position.x >> characterClient.position.y 
		>> dir >> act >> characterClient.nbPoints >> characterClient.youwin;

	characterClient.chDirection = (characterDirection)dir;
	characterClient.chAction = (characterAction)act;

	return packet;
}

CharacterClient::CharacterClient(sf::Color charactColor) {
	string fileName, temp;
	characterColor = charactColor;
	sequence = 0;
	chDirection = RIGHT;
	chAction = FACING;
	nbPoints = 0;

	vitesseMove = sf::milliseconds(50);
	vitesseFace = sf::milliseconds(100);
	timeBeforeFace = sf::milliseconds(3000);	
	count = 0;
	chronoTime.restart();


	/*Initialisation des matrices comboBox*/
	sf::Vector2f matriceTemp[8]=	{
		sf::Vector2f(4,4),
		sf::Vector2f(37,3),
		sf::Vector2f(69,2),
		sf::Vector2f(100,3),
		sf::Vector2f(132,4),
		sf::Vector2f(165,3),
		sf::Vector2f(197,2),
		sf::Vector2f(228,3)};

	memcpy(textureRunLeftSequence, matriceTemp, sizeof(sf::Vector2f) * 8);

	sf::Vector2f matriceTemp2[8] = {
		sf::Vector2f(6,4),
		sf::Vector2f(40,3),
		sf::Vector2f(73,2),
		sf::Vector2f(104,3),
		sf::Vector2f(134,4),
		sf::Vector2f(168,3),
		sf::Vector2f(201,2),
		sf::Vector2f(232,3)};

	memcpy(textureRunRightSequence, matriceTemp2, sizeof(sf::Vector2f) * 8);

	sf::Vector2f matriceTemp3[8] = {
		sf::Vector2f(4,2),
		sf::Vector2f(38,2),
		sf::Vector2f(70,2),
		sf::Vector2f(102,2),
		sf::Vector2f(134,2),
		sf::Vector2f(166,2),
		sf::Vector2f(198,2),
		sf::Vector2f(230,2)};

	memcpy(textureFaceLeftSequence, matriceTemp3, sizeof(sf::Vector2f) * 8);

	sf::Vector2f matriceTemp4[8] = {
		sf::Vector2f(6,2),
		sf::Vector2f(38,2),
		sf::Vector2f(70,2),
		sf::Vector2f(102,2),
		sf::Vector2f(134,2),
		sf::Vector2f(166,2),
		sf::Vector2f(198,2),
		sf::Vector2f(230,2)};

	memcpy(textureFaceRightSequence, matriceTemp4, sizeof(sf::Vector2f) * 8);

	sf::Vector2f matriceTemp5[4] = {
		sf::Vector2f(1, 3),
		sf::Vector2f(34, 3),
		sf::Vector2f(65, 3),
		sf::Vector2f(97, 3)
	};
	memcpy(textureDeadSequence, matriceTemp5, sizeof(sf::Vector2f) * 4);
	
	textureJumpLeft = sf::Vector2f(1,2);
	textureJumpRight = sf::Vector2f(4,2);


	/*Chargement des textures pour les personnages*/
	if(characterColor == sf::Color::Blue) {
		extension = "_blue.png";
	}
	else {
		extension = "_red.png";
	}

	temp = "../Img/character/run_right";
	fileName = temp + extension;	

	/*Texture du personnage marchant vers la droite*/
	if (!characterRunRight.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage marchant vers la gauche*/
	temp = "../Img/character/run_left";
	fileName = temp + extension;	

	if (!characterRunLeft.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage regardant vers la droite*/
	temp = "../Img/character/facing_right";
	fileName = temp + extension;	

	if (!characterFacingRight.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage regardant vers la gauche*/
	temp = "../Img/character/facing_left";
	fileName = temp + extension;	

	if (!characterFacingLeft.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage sautant vers la gauche*/
	temp = "../Img/character/jumprun_left";
	fileName = temp + extension;	

	if (!characterJumpingLeft.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage sautant vers la droite*/
	temp = "../Img/character/jumprun_right";
	fileName = temp + extension;	

	if (!characterJumpingRight.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}

	/*Texture du personnage sautant vers la droite*/
	temp = "../Img/character/die";
	fileName = temp + extension;	

	if (!characterDead.loadFromFile(fileName)) {
		cout << "Erreur au chargement de la texture" << endl;
	}
}

/*Dessine le personnage dans la fenetre w*/
void CharacterClient::draw(sf::RenderWindow& w) {
	if (chAction == DEAD) {
		characterSprite.setTexture(characterDead, true);
				characterSprite.setTextureRect(sf::IntRect(textureDeadSequence[sequence].x,
				textureDeadSequence[sequence].y, 30, 27));
	} else {

		if ( chDirection == RIGHT ) {

		/*Le personnage cour vers la droite*/
			if(chAction == RUNNING) {
				characterSprite.setTexture(characterRunRight, true);
				characterSprite.setTextureRect(sf::IntRect(textureRunRightSequence[sequence].x,
				textureRunRightSequence[sequence].y,27, 30));
			}
			/*Le personnage regarde a droite a l'arret*/
			if(chAction == FACING) {
				characterSprite.setTexture(characterFacingRight);
				characterSprite.setTextureRect(sf::IntRect(textureFaceRightSequence[sequence].x,
					textureFaceRightSequence[sequence].y,27, 30));
			}
			/*Le personnage regarde a droite en saut*/
			if(chAction == JUMPING || chAction == JUMPRUNNING) {
				characterSprite.setTexture(characterJumpingRight);
				characterSprite.setTextureRect(sf::IntRect(textureJumpRight.x,
					textureJumpRight.y,27, 30));
			}


		} else if ( chDirection == LEFT ) {
			/*Le personnage cour vers la gauche*/
			if(chAction == RUNNING) {
				characterSprite.setTexture(characterRunLeft);
				characterSprite.setTextureRect(sf::IntRect(textureRunLeftSequence[sequence].x,
					textureRunLeftSequence[sequence].y,27, 30));
			}
			/*Le personnage regarde a gauche a l'arret*/
			if(chAction == FACING) {
				characterSprite.setTexture(characterFacingLeft);
				characterSprite.setTextureRect(sf::IntRect(textureFaceLeftSequence[sequence].x,
				textureFaceLeftSequence[sequence].y,27, 30));
			}
			/*Le personnage regarde a gauche en saut*/
			if(chAction == JUMPING || chAction == JUMPRUNNING) {
				characterSprite.setTexture(characterJumpingLeft);
				characterSprite.setTextureRect(sf::IntRect(textureJumpLeft.x,
					textureJumpLeft.y,27, 30));
			}
		} 
	}

	/*Place le sprite a la position du personnage*/
	characterSprite.setOrigin(20.0/2, 27.0/2); 
	characterSprite.setPosition(position.x, position.y);

	w.draw(characterSprite);

}

void CharacterClient::receive(sf::TcpSocket& socket) {
		sf::Packet packet;

		characterAction ca = chAction;

		socket.receive(packet);
		packet >> *this;

		/*Si on passe d'un etat RUNNING a un etat FACING*/
		if(ca != FACING && chAction == FACING) {

			count = 0;
			sequence = 0;
			chronoTime.restart();
			chronoFace.restart();

		}
		/*Si on passe d'un etat FACING a un etat RUNNING*/
		if(ca == FACING && chAction == RUNNING) {

			sequence = 0;
			time = chronoTime.restart();
		}
}



void CharacterClient::animationPlayer1() {
	time = chronoTime.getElapsedTime();
	timeFace = chronoFace.getElapsedTime();
		
	//Calcul du temps avant d'executer un nouveau deplacement
	if(time > vitesseMove && (chAction == RUNNING || chAction == DEAD)) {
		incSequence();
		chronoTime.restart();
	}

	
	//Calcul du temps avant d'executer une animation FACING
	if(timeFace > timeBeforeFace && chAction == FACING) {
		//Calcul de la vitesse de l'animation FACING
		if(time > vitesseFace && chAction == FACING) {
			incSequence();
			chronoTime.restart();
			count++;
		}

		if(count == 8 ) {
			count = 0;
			chronoFace.restart();
		}
	}
}

void CharacterClient::incSequence() {
	sequence++;

	if(( chAction == RUNNING || chAction == FACING ) && sequence == 8) 
		sequence = 0;

	if( chAction == DEAD && sequence == 4) 
		sequence = 0;

}

bool CharacterClient::hasWon() {
	return youwin;
}

